<template>
  <div id="invoice">
    <top-bar :title="title" v-show="IsTopbar"></top-bar>
    <div class="invoice_hd">发票详情</div>
    <div class="invoice_centent">
      <md-field>
        <md-field-item name="抬头类型" title="抬头类型">
          <p class="invoice_type">
            <span @click="enterprise">
              <i :class="{active:invoice_type == 'enterprise'}"></i>企业单位
            </span>
            <span @click="personal">
              <i :class="{active:invoice_type == 'personal'}"></i>个人/非企业单位
            </span>
          </p>
        </md-field-item>
      </md-field>
      <p class="rise_text_box">
        <label>发票抬头</label>
        <input
          :value="rise_text"
          @input="invoiceTop"
          @compositionstart="start"
          @compositionend="end"
          placeholder="请输入发票抬头"
        />
      </p>
      <div class="invoice_text_box" v-show="IsinvoiceTop">
        <div class="invoice_text_header">
          <ul>
            <li v-for="(item,index) in invoiceTopArr" :key="index" @click="getInvoiceTop(item.cpy_name,item.tax_no)">
              <h6>{{item.cpy_name}}</h6>
              <p>{{item.tax_no}}</p>
            </li>
          </ul>
        </div>
      </div>
      <md-field v-show="IsNumber">
        <md-input-item ref="纳税人识别号" title="纳税人识别号" placeholder="请输入纳税人识别号" align="right" v-model="rise_number"></md-input-item>
      </md-field>
      <md-field>
        <md-field-item name="发票内容" title="发票内容" align="right">*运输服务*平安到家-出行服务</md-field-item>
      </md-field>
      <md-field>
        <md-field-item name="开票金额" title="开票金额" align="right">
          <p style="color:#FF6917">{{riseMoney}}元</p>
        </md-field-item>
      </md-field>
      <md-field>
        <md-field-item name="更多信息" title="更多信息" arrow="arrow-right" align="right" @click="invoiceMore">开户行、备注等</md-field-item>
      </md-field>
    </div>
    <div class="invoice_hd">电子发票预计在1～3个工作日内寄出</div>
    <div class="invoice_centent">
      <md-field>
        <md-input-item ref="电子邮件" title="电子邮件" placeholder="填写电子邮箱，用于接收电子发票" align="right" v-model="invoice_email"></md-input-item>
      </md-field>
    </div>
    <md-popup v-model="isinvoiceMoreShow" position="bottom">
      <md-popup-title-bar cancel-text="取消" @cancel="hidePopUp"></md-popup-title-bar>
      <div>
        <md-field>
          <md-input-item ref="备注名称" title="备注名称" placeholder="请填写备注名称" align="right" v-model="remarksr_name"></md-input-item>
        </md-field>
        <md-field>
          <md-input-item ref="地址和电话" title="地址和电话" placeholder="请填写地址和电话" align="right" v-model="address_phone"></md-input-item>
        </md-field>
        <md-field>
          <md-input-item ref="开户行和账户" title="开户行和账户" placeholder="请填写开户行和账户" align="right" v-model="bank_name"></md-input-item>
        </md-field>
        <p class="invoiceMoreBtn" @click="invoiceMoreBtn">确定</p>
      </div>
    </md-popup>
    <div class="invoice_ct_btn">
      <p @click="invoiceListBtn">提交</p>
    </div>
    <md-popup v-model="isinvoiceListShow" position="bottom">
      <md-popup-title-bar title="开具电子发票" cancel-text="取消" @cancel="hidePopUp"></md-popup-title-bar>
      <div class="invoiceListText_bg">
        <p class="invoiceListText_hd">{{invoiceListText_hd}}</p>
        <p class="invoiceListText">
          发票类型
          <span>电子发票</span>
        </p>
        <p class="invoiceListText">
          发票抬头
          <span>{{rise_text}}</span>
        </p>
        <p class="invoiceListText" v-show="IsNumber">
          纳税人识别号
          <span>{{rise_number}}</span>
        </p>
        <p class="invoiceListText">
          发票内容
          <span>*运输服务*平安到家-出行服务</span>
        </p>
        <p class="invoiceListText">
          电子邮件
          <span>{{invoice_email}}</span>
        </p>
        <p class="invoiceListText">
          开票金额
          <span>{{riseMoney}}元</span>
        </p>
        <p class="confirminvoiceBtn" @click="confirminvoiceBtn">确认提交</p>
      </div>
    </md-popup>
  </div>
</template>
<script>
import '@/assets/svgs/zfcg_icon.svg';
import {
  Tabs,
  Icon,
  Field,
  FieldItem,
  InputItem,
  Toast,
  Popup,
  PopupTitleBar,
  Radio,
  Dialog
} from 'mand-mobile';
import TopBar from '@/components/TopBar';
import Loading from '@/components/loading';
import Utils from '@/common/pakjUtils';
import { decryptByDESModeEBC, encryptByDESModeEBC } from '@/common/encrypt';
import { mapGetters, mapActions, mapMutations } from 'vuex';
import http from '@/common/http';
var CryptoJS = require('crypto-js');
export default {
  components: {
    [Tabs.name]: Tabs,
    [Icon.name]: Icon,
    [TopBar.name]: TopBar,
    [Field.name]: Field,
    [Toast.name]: Toast,
    [Dialog.name]: Dialog,
    [Radio.name]: Radio,
    [FieldItem.name]: FieldItem,
    [Popup.name]: Popup,
    [PopupTitleBar.name]: PopupTitleBar,
    [InputItem.name]: InputItem
  },
  data() {
    return {
      title: '开具发票',
      composing: false,
      invoice_type: 'enterprise',
      IsNumber: true, // 是否显示纳税人识别号
      rise_text: '', // 发票抬头
      rise_number: '', // 纳税人识别号
      invoice_email: '', // 邮箱地址
      riseValue: '2', // 抬头类型 1个人  2 企业单位
      IsinvoiceTop: false,
      invoiceTopArr: '',
      riseMoney: sessionStorage['money'],
      isinvoiceMoreShow: false,
      remarksr_name: '',
      address_phone: '',
      bank_name: '',
      isinvoiceListShow: false,
      IsTopbar: true,
      lastInputStamp: 0,
      invoiceListText_hd:
        '请确认发票抬头及纳税识别号正确无误，发票开具后无法更换。 请确认邮箱无误，电子发票将在系统开具后发送发送至您的邮箱，请注意查收'
    };
  },
  beforeMount() {
    // 获取开具发票历史信息
    this.getInvHistory();
    if (Utils.isApp()) {
      // 判断当前环境是否在app
      this.IsTopbar = false;
    }
  },
  methods: {
    getInvHistory() {
      Loading.show();
      http.post('pakj/invoice/getHitoryInvoice').then(res => {
        Loading.hide();
        console.log(res.data);
        if (res.data.code === '000000') {
          if (res.data.data.length > 0) {
            this.rise_number = res.data.data[0].buyerTaxNo;
            this.rise_text = res.data.data[0].buyerName;
            this.invoice_email = res.data.data[0].buyerEmail;
            this.address_phone = res.data.data[0].buyerAddressPhone;
            this.bank_name = res.data.data[0].buyerBankAccount;
          }
        } else {
          Dialog.alert({
            title: '提示',
            content: res.data.msg,
            confirmText: '确定'
          });
        }
      });
    },
    enterprise() {
      this.getInvHistory();
      this.invoice_type = 'enterprise';
      this.IsNumber = true;
      this.riseValue = '2';
      this.invoiceListText_hd =
        '请确认发票抬头及纳税识别号正确无误，发票开具后无法更换。 请确认邮箱无误，电子发票将在系统开具后发送发送至您的邮箱，请注意查收';
    },
    personal() {
      this.rise_number = ''; // 选择个人时清空税号
      this.invoice_type = 'personal';
      this.IsNumber = false;
      this.riseValue = '1';
      this.invoiceListText_hd =
        '您选择的发票抬头类型为个人，如需报销，请选择企业单位抬头发票。 请确认邮箱无误，电子发票将在系统开具后发送发送至您的邮箱，请注意查收。';
    },
    invoiceMore() {
      this.isinvoiceMoreShow = true;
    },
    hidePopUp() {
      this.isinvoiceMoreShow = false;
      this.isinvoiceListShow = false;
    },
    invoiceMoreBtn() {
      this.isinvoiceMoreShow = false;
    },
    // 输入抬头查询企业识别号
    invoiceTop($event) {
      if (this.composing) {
        return;
      }
      this.rise_text = $event.target.value;
      let now = new Date().getTime();
      if (now - this.lastInputStamp < 1000) {
        return;
      }
      this.lastInputStamp = now;
      if (this.rise_text.length > 4) {
        this.IsinvoiceTop = true;
        let params = {
          cpy_name: this.rise_text
        };
        http.post('pakj/invoice/getCompanyByName', params).then(res => {
          if (res.data.code === '000000') {
            console.log(res.data);
            this.invoiceTopArr = res.data.data;
          } else {
            Dialog.alert({
              title: '提示',
              content: res.data.msg,
              confirmText: '确定'
            });
          }
        });
      } else {
        this.IsinvoiceTop = false;
      }
    },
    start() {
      this.composing = true;
    },
    end($event) {
      this.composing = false;
      this.rise_text = $event.target.value;
      this.invoiceTop($event);
    },
    getInvoiceTop(name, number) {
      if (this.riseValue === '2') {
        this.rise_number = number;
      }
      this.rise_text = name;
      this.IsinvoiceTop = false;
    },
    // 提交
    invoiceListBtn() {
      var reg = /^[\u2E80-\u9FFF]+$/; // 汉字正则
      if (!this.rise_text) {
        Toast({
          content: '请输入发票抬头',
          duration: 3000,
          hasMask: true
        });
        return false;
      }
      if (this.rise_text.indexOf(' ') >= 0) {
        Toast({
          content: '请不要输入空格',
          duration: 3000,
          hasMask: true
        });
        return false;
      }
      if (this.IsNumber) {
        if (!this.rise_number) {
          Toast({
            content: '请输入纳税人识别号',
            duration: 3000,
            hasMask: true
          });
          return false;
        } else {
          if (!/^([0-9A-Z]){15}$|^([0-9A-Z]){18}$|^([0-9A-Z]){20}$/.test(this.rise_number)) {
            Toast({
              content: '纳税人识别号格式错误',
              duration: 3000,
              hasMask: true
            });
            return false;
          }
        }
      }
      if (!this.invoice_email.trim()) {
        Toast({
          content: '请输入电子邮箱',
          duration: 3000,
          hasMask: true
        });
        return false;
      } else {
        if (!/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(this.invoice_email.trim())) {
          Toast({
            content: '电子邮箱格式错误',
            duration: 3000,
            hasMask: true
          });
          return false;
        }
      }
      this.isinvoiceListShow = true;
    },
    // 确认提交
    confirminvoiceBtn() {
      var orderNo = sessionStorage['orderNoList']; // 接受的唯一行程订单号
      var params = {
        type: this.riseValue, // 发票类型 抬头类型 1个人  2 企业单位
        cpy_name: this.rise_text.trim(), // 发票抬头
        tax_no: this.rise_number.trim(), // 纳税人识别号
        email: this.invoice_email.trim(), // 邮箱地址
        ads_phone: this.address_phone.trim(), // 单位地址和联系电话
        bank_act: this.bank_name.trim(), // 开户行和开户行账号
        remark: this.remarksr_name.trim(), // 备注
        money: this.riseMoney, // 发票金额
        order_no: orderNo // 勾选的订单编号
      };
      Loading.show();
      http.post('pakj/invoice/saveUserInvoice', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          console.log(res.data);
          var invoiceId = res.data.data;
          Dialog.alert({
            icon: 'zfcg_icon',
            content: '提交成功',
            confirmText: '我知道了',
            onConfirm: () => {
              this.$router.push({
                name: 'invoiceDetails',
                query: {
                  invoiceId: invoiceId
                }
              });
            }
          });
        } else {
          Dialog.alert({
            title: '提示',
            content: res.data.msg,
            confirmText: '确定'
          });
        }
      });
    }
  }
};
</script>
<style lang="stylus">
p {
  display: block;
  margin-block-start: 0px;
  margin-block-end: 0px;
  margin-inline-start: 0px;
  margin-inline-end: 0px;
}

h6 {
  display: block;
  font-size: 22px;
  margin-block-start: 0em;
  margin-block-end: 0em;
  margin-inline-start: 0px;
  margin-inline-end: 0px;
  font-weight: 400;
}

.md-input-item .md-input-item-extra.fixed, .md-input-item .md-input-item-title.fixed {
  width: 200px;
}

#invoice {
  height: 100vh;
  background-color: color-primary-background;
}

.invoice_hd {
  height: 80px;
  line-height: 80px;
  font-size: 26px;
  color: #ccc;
  padding-left: 30px;
  background-color: #EFEFEF;
}

.invoice_centent {
  background-color: #fff;
}

.invoice_ct_btn {
  margin-top: 15vh;
  bottom: 20px;
  width: 100%;
}

.invoice_ct_btn p {
  margin: 0px 20px;
  background-color: #FF6917;
  height: 98px;
  line-height: 98px;
  font-size: 36px;
  color: #fff;
  text-align: center;
  border-radius: 10px;
}

.rise_text_box {
  padding: 0px 30px;
  height: 85px;
  line-height: 85px;
}

.rise_text_box label {
  font-size: 32px;
  color: #333;
  height: 68px;
  line-height: 68px;
}

.rise_text_box input {
  text-align: right;
  border: none;
  outline: none;
  font-size: 28px;
  float: right;
  width: 60%;
  height: 60px;
  line-height: 60px;
}

.invoice_type {
  font-size: 28px;
  color: #333;
}

.invoice_type span {
  margin-left: 40px;
}

.invoice_type span i {
  display: inline-block;
  background-color: #fff;
  width: 35px;
  height: 35px;
  border: 1px solid #e3e3e3;
  border-radius: 50%;
  position: relative;
  top: 8px;
  right: 10px;
}

.invoice_type span i.active {
  background: url('../../assets/imgs/ddzf_icon_pre@2x.png') no-repeat center center;
  background-size: contain;
  border-color: #fff;
  width: 36px;
  height: 36px;
}

.invoiceMoreBtn {
  background-color: #FF6917;
  height: 98px;
  line-height: 98px;
  font-size: 36px;
  color: #fff;
  text-align: center;
}

.invoiceListText_bg {
  background-color: #fff;
}

.invoiceListText_hd {
  padding: 20px 30px;
  font-size: 22px;
  color: #FF6917;
  line-height: 40px;
}

.invoiceListText {
  font-size: 30px;
  color: #999;
  height: 88px;
  line-height: 88px;
  padding: 0px 30px;
}

.invoiceListText span {
  float: right;
  color: #333;
}

.invoice_text_box {
  position: relative;
}

.invoice_text_header {
  background-color: #EFEFEF;
  position: absolute;
  top: 0px;
  left: 0px;
  width: 100%;
  z-index: 10;
  height: 450px;
  overflow: hidden;
  overflow-y: auto;
}

.invoice_text_header ul li {
  list-style: none;
  text-align: right;
  padding: 0px 30px;
  border-top: solid 1px #E0E0E0;
  height: 100px;
}

.invoice_text_header ul li h6 {
  font-size: 28px;
  color: #333;
  font-weight: 400;
  margin-top: 10px;
}

.invoice_text_header ul li p {
  font-size: 26px;
  color: #999;
  margin-top: 10px;
}

.confirminvoiceBtn {
  background-color: #FF6917;
  height: 98px;
  line-height: 98px;
  font-size: 36px;
  color: #fff;
  text-align: center;
  margin: 0px 30px;
  border-radius: 10px;
}
</style>
